﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models.WeChat.Dtos.QYDepartment;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 企业号组织
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-08-06 
    /// </summary>
    [Route("api/WeChat/QYDepartment")]
    public class QYDepartmentController : BaseApiController
    {
        private QYDepartmentBll qYDepartmentBll = new QYDepartmentBll();
        private OrganizeBll organizeBll = new OrganizeBll();

        #region Get
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] KeywordInput input)
        {
            var data = await qYDepartmentBll.GetList();
            var treeList = data.MapToList<QYDepartmentListOutput>();
            if (input.keyword.IsNotEmptyOrNull())
            {
                treeList = treeList.TreeWhere(t => t.fullName.Contains(input.keyword) || t.enCode.Contains(input.keyword), t => t.id, t => t.parentId);
            }
            return Success(new { list = treeList.ToTree("-1") });
        }
        #endregion

        #region Post
        /// <summary>
        /// 同步部门
        /// </summary>
        /// <returns></returns>
        [HttpPost("Actions/Synchronization")]
        [AllowAnonymous]
        public async Task<ResponseResult> Synchronization()
        {
            try
            {
                var data = await organizeBll.GetList();
                if (data != null && data.Count > 0)
                {
                    await qYDepartmentBll.Synchronization(data);
                }
                return Success("同步成功");
            }
            catch (Exception)
            {

                return Error("操作过于频繁");
            }
        }
        #endregion
    }
}
